package edu.nf.waimai.dao.order.impl;

import edu.nf.util.DBUtils;
import edu.nf.waimai.dao.order.OrderDAO;
import edu.nf.waimai.model.order.Order;
import edu.nf.waimai.model.order.ShopOrder;
import edu.nf.waimai.model.order.UserOrder;
import edu.nf.waimai.model.shop.Shop;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanHandler;
import org.nf.db.util.result.BeanListHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2023/6/26
 * @Author zgm
 */
public class OrderDAOImpl implements OrderDAO {

    @Override
    public List<UserOrder> listOrder(Integer uid, String orderType) {
        SqlExecutor executor = new SqlExecutor(DBUtils.getConnection());
        String sql = "SELECT o.order_id, o.u_id,o.rider_id, o.shop_id,o.address_id, o.order_type,s.shop_name,s.shop_img,o.order_status, o.total_price, o.order_datetime,o.order_remarks ,o.order_details " +
                " FROM orders o JOIN shop s ON o.shop_id = s.shop_id " +
                " WHERE o.u_id = ? and o.order_type = ? ORDER BY o.order_datetime desc";
        return executor.executeQuery(sql,new BeanListHandler<>(UserOrder.class),uid,orderType);
    }

    @Override
    public List<ShopOrder> listShopOrder(Integer shopId, Integer orderStatus) {
        SqlExecutor executor = new SqlExecutor(DBUtils.getConnection());
        String sql = "SELECT o.order_id,o.u_id,o.shop_id,o.address_id,o.rider_id,o.order_status,o.total_price,o.order_type,o.order_datetime,o.order_remarks,o.order_details," +
                    " u.u_nickname,u.u_phone,s.shop_name,s.shop_phone,s.shop_address,a.province,a.city, a.district ,a.detail " +
                    " FROM orders o " +
                    " JOIN users u ON u.u_id = o.u_id " +
                    " JOIN shop s ON o.shop_id = s.shop_id " +
                    " JOIN address a on a.address_id = o.address_id" +
                    " WHERE o.shop_id = ? and o.order_status = ? ORDER BY o.order_datetime desc";
        return executor.executeQuery(sql,new BeanListHandler<>(ShopOrder.class),shopId,orderStatus);
    }

    @Override
    public List<ShopOrder> listShopIsRiderOrder(Integer shopId, Integer orderStatus) {
        SqlExecutor executor = new SqlExecutor(DBUtils.getConnection());
        String sql = "SELECT o.order_id,o.u_id,o.shop_id,o.address_id,o.rider_id,o.order_status,o.total_price,o.order_type,o.order_datetime,o.order_remarks,o.order_details,\n" +
                " u.u_nickname,u.u_phone,s.shop_name,s.shop_phone,s.shop_address,r.rider_name,r.rider_phone,r.rider_type ,a.province,a.city, a.district ,a.detail" +
                " FROM orders o " +
                " JOIN users u ON u.u_id = o.u_id " +
                " JOIN shop s ON o.shop_id = s.shop_id " +
                " JOIN rider r ON o.rider_id = r.rider_id " +
                " JOIN address a on a.address_id = o.address_id" +
                " WHERE o.shop_id = ? and o.order_status = ? ORDER BY o.order_datetime desc";
        return executor.executeQuery(sql,new BeanListHandler<>(ShopOrder.class),shopId,orderStatus);
    }

    @Override
    public int updateOrder(String orderId, Integer status) {
        SqlExecutor executor = new SqlExecutor(DBUtils.getConnection());
        String sql = "UPDATE orders SET order_status = ? WHERE order_id = ? ;";
        return executor.executeUpdate(sql,status,orderId);
    }

    @Override
    public int addOrder(Order order) {
        SqlExecutor executor = new SqlExecutor(DBUtils.getConnection());
        String sql = "insert into orders(order_id,u_id,shop_id,rider_id,address_id,total_price,order_type,order_remarks,order_details)" +
                " VALUES(?,?,?,?,?,?,?,?,?);";
        return executor.executeUpdate(sql,order.getOrderId(),
                order.getUid(),
                order.getShopId(),
                order.getRiderId(),
                order.getAddressId(),
                order.getTotalPrice(),
                order.getOrderType(),
                order.getOrderRemarks(),
                order.getOrderDetails());
    }

    @Override
    public UserOrder getUserOrder(Integer uid) {
        SqlExecutor executor = new SqlExecutor(DBUtils.getConnection());
        String sql = "SELECT order_id,u_id,shop_id,address_id,rider_id,order_status,total_price,order_type,order_datetime,order_remarks, order_details FROM orders " +
                " where u_id = ? ORDER BY order_datetime DESC LIMIT 1;";
        return executor.executeQuery(sql,new BeanHandler<>(UserOrder.class),uid);
    }

    @Override
    public UserOrder getShopOrder(String orderId) {
        SqlExecutor executor = new SqlExecutor(DBUtils.getConnection());
        String sql = "SELECT order_id,u_id,shop_id,address_id,rider_id,order_status,total_price,order_type,order_datetime,order_remarks, order_details FROM orders where order_id = ?";
        return executor.executeQuery(sql,new BeanHandler<>(UserOrder.class),orderId);
    }

}
